package com.jd.org.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.service.IEmployeeService;
import com.jd.user.domain.Logininfo;
import com.jd.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
// 查询的方法比较多，使用查询的事务配置
// 只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
// @Inherited 在@Transactional有这个注解表示可以被继承，而@Service不能被继承，子类必须写
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    @Transactional
    public void add(Employee employee) {
        // 对密码和盐值进行添加
        String salt = RandomUtil.randomString(32);
        String md5 = SecureUtil.md5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);
        // 新建一个登陆信息对象获取自增长id
        Logininfo logininfo = new Logininfo();
        // 用bean工具类复制，会将同名字段进行赋值
        BeanUtils.copyProperties(employee, logininfo); // 此时employee没有添加，logininfo id 为null
        logininfo.setType(0); // 设置type为员工
        logininfoMapper.add(logininfo); // 添加获取id
        employee.setLogininfoId(logininfo.getId()); // 设置logininfoid
        super.add(employee);
        // 维护中间表t_employee_role
        if (employee.getRoleId()!=null){
            employeeMapper.addToEmpRole(employee);
        }
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        // 获取旧的employee信息
        Employee oldemployee = employeeMapper.findOne(employee.getId());
        // 密码盐值 loginId 不变
        employee.setSalt(oldemployee.getSalt());
        employee.setPassword(oldemployee.getPassword());
        employee.setLogininfoId(oldemployee.getLogininfoId());
        employee.setShopId(oldemployee.getShopId());
        super.update(employee);
        // 获取旧的logininfo信息
        Logininfo oldLogin = logininfoMapper.findOne(oldemployee.getLogininfoId());
        BeanUtils.copyProperties(employee, oldLogin);
        oldLogin.setId(oldemployee.getLogininfoId());
        logininfoMapper.update(oldLogin);
        // 维护中间表t_employee_role 修改先删在加
        employeeMapper.delByEmpId(employee.getId());
        if (employee.getRoleId()!=null){
            employeeMapper.addToEmpRole(employee);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        Employee employee = employeeMapper.findOne(id);
        // 先删除中间表，在删除主表信息
        employeeMapper.delByEmpId(employee.getId());
        logininfoMapper.del(employee.getLogininfoId());
        super.del(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }
}
